package com.ldu.exam.service;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ldu.exam.assistant.BeanAssistant;
import com.ldu.exam.dao.ExamRepository;
import com.ldu.exam.model.ExamModel;

@Service
public class ExamService {
	@Autowired ExamRepository repository;
	
	public List<ExamModel> findNoPage()
	{
		List<ExamModel> list = this.repository.findAll();
		return list;
	}
	
	public Page<ExamModel> findAll(Pageable page)
	{
		return this.repository.findAll(page);
	}
	
	public Object add(ExamModel model)
	{
		return this.repository.save(model);
	}
	
	public void delete(String id) 
	{
		ExamModel em = new ExamModel();
		em.setId(id);
		this.repository.delete(em);
	}
	
	public Object update(String id,ExamModel model)
	{
		model.setId( null);
		
		ExamModel origin = this.repository.findById(id).orElse(null) ;
		
		String [] nullPropertyNames = BeanAssistant.getNullPropertyNames( model) ;
		
		BeanUtils.copyProperties( model, origin, nullPropertyNames);
		
		return this.repository.save( origin);
	}
}
